package org.spring.springboot.app.order.harddeviceorder.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.order.harddeviceorder.domain.po.DeviceOrderPO;
import org.spring.springboot.app.order.harddeviceorder.domain.vo.DeviceOrderSearchVO;
import org.spring.springboot.app.order.harddeviceorder.domain.vo.DeviceOrderVO;
import org.spring.springboot.app.order.harddeviceorder.domain.vo.DeviceOrdersaveVO;
import org.spring.springboot.app.order.harddeviceorder.service.IDeviceorderService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@Api(description = "声波订单操作接口") //,tags = ApiIndex.DeviceorderController
@RequestMapping(value = "/api/deviceorder")
@RestController
public class DeviceOrderController {

    @Autowired
    private IDeviceorderService harddeviceorderservice;

    @ApiOperation(value = "查询声波订单列表-分页")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<PageInfo<List<DeviceOrderVO>>> findPage(
            @ApiParam(value = "下单人", required = false) @RequestParam(defaultValue = "") String username,
            @ApiParam(value = "订单状态", required = false) @RequestParam(defaultValue = "") String state,
            @ApiParam(value = "订单号", required = false) @RequestParam(defaultValue = "") String orderId,
            @ApiParam(value = "名称", required = false) @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(value = "订单状态", required = true) @RequestParam(defaultValue = "") String type,
            @ApiParam(value = "开始创建时间（例如1993-08-11 或者 1993-08-11 00:00:00）", required = false) @RequestParam(defaultValue = "") String startCreateDate,
            @ApiParam(value = "结束创建时间（例如1993-08-11 或者 1993-08-11 23:59:59）", required = false) @RequestParam(defaultValue = "") String endCreateDate
    ) {
        DeviceOrderSearchVO searchvo=new DeviceOrderSearchVO();  //查询对象
        if(StringUtils.isNotBlank(startCreateDate)){
            if(startCreateDate.indexOf(":")==-1){
                startCreateDate=startCreateDate+" 00:00:00";
            }
        }
        if(StringUtils.isNotBlank(endCreateDate)){
            if(endCreateDate.indexOf(":")==-1){
                endCreateDate=endCreateDate+" 23:59:59";
            }
        }
        searchvo.setStartCreateDate(startCreateDate);
        searchvo.setEndCreateDate(endCreateDate);
        searchvo.setType(type);
        searchvo.setName(name);
        searchvo.setOrderId(orderId);
        searchvo.setState(state);
        searchvo.setUsername(username);
        R<PageInfo<List<DeviceOrderVO>>> result = new R<PageInfo<List<DeviceOrderVO>>>();
        List<DeviceOrderVO> _list = harddeviceorderservice.findPage(searchvo,pageNum, pageSize);
        PageInfo p =new PageInfo(_list);
        p.setList(getSysDictVOS(p.getList()));
        result.setData(p);
        return result;
    }

    private List<DeviceOrderVO> getSysDictVOS(List<DeviceOrderVO> _list) {
        List<DeviceOrderVO> list = new ArrayList<DeviceOrderVO>();
        if (_list != null && _list.size()>0) {
            for (DeviceOrderVO po : _list) {
                DeviceOrderVO vo = new DeviceOrderVO();
                BeanUtils.copyProperties(po,vo);
                list.add(vo);
            }
        }
        return list;
    }

    @ApiOperation(value = "根据Id获取单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<DeviceOrderPO> find(
            @ApiParam(value = "声波订单id", required = true) @PathVariable("id") String id
    ) {
        R<DeviceOrderPO> result = new R<DeviceOrderPO>();
        DeviceOrderPO po = harddeviceorderservice.find(id);
        DeviceOrderPO vo = new DeviceOrderPO();
        BeanUtils.copyProperties(po,vo);
        result.setData(vo);
        return result;
    }

    @ApiOperation(value = "保存声波订单信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(
            @ApiParam(value = "声波订单对象", required = true) @RequestBody DeviceOrdersaveVO deviceOrdersaveVO,
            @ApiIgnore User user
    ) {
        R result = new R();
        DeviceOrderPO po = new DeviceOrderPO();
        BeanUtils.copyProperties(deviceOrdersaveVO,po);
        int code = harddeviceorderservice.save(po);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("保存失败");
        }
        return result;
    }


    @ApiOperation(value = "修改声波订单信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(
            @ApiParam(value = "广告机对象", required = true) @RequestBody DeviceOrdersaveVO deviceOrdersaveVO,
            @ApiIgnore User user
    ) {
        R result = new R();
        DeviceOrderPO po = new DeviceOrderPO();
        BeanUtils.copyProperties(deviceOrdersaveVO,po);
        int code = harddeviceorderservice.update(po);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("修改失败");
        }
        return result;
    }

    @ApiOperation(value = "删除声波订单信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(
            @ApiParam(value = "广告机id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        int code = harddeviceorderservice.delete(id);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("删除失败");
        }
        return result;
    }

    /**
     *  批量删除
     * @param ids
     */
    @ApiOperation(value="批量删除声波订单信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public R deletes(@ApiParam(value = "广告机ids(用,分割)", required = true) @RequestParam("ids") String ids) {
        R result = new R();
        int code = harddeviceorderservice.deletes(ids);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("刪除失败");
        }
        return result;
    }
}
